package fy.uap.operatorImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.oainterface.exportData.flow.FlowExport;
import com.seeyon.oainterface.exportData.form.ValueExport;

import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogWriter;
import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.set.OA_UAPAccountContrast;
import fy.msg.MessageSender;
import fy.uap.bean.FundsBean;
import fy.uap.dao.FundsDao;
import fy.util.MyUtil;

public class FundsOperator extends AForm_Save_Operator {

	private FundsBean fundsBean = new FundsBean();

	private String pk_corp;
	private String oldBill_amount;
	private String newBill_amount;
	private short form_type;

	private List<String> p_dept_list = new ArrayList<String>(); // 费用部门
	private List<String> cp_dept_list = new ArrayList<String>(); // 差旅 费用部门
	private List<String> p_type_list = new ArrayList<String>();// 费用类型
	private List<String> p_cost_list = new ArrayList<String>();// 费用

	private final static short FORM_BAOXIAOFENGMIAN = 1;
	private final static short FORM_CHAILVFEI = 2;
	private final static short FORM_JIEKUANDAN = 3;
	private final static short FORM_HUIKUANDAN = 4;

	private static Log log = LogFactory.getLog(FundsOperator.class);

	// private static String[] corpArray = {"1025","1024", "1002", "1004",
	// "1005" };

	public String getFormName() {
		return "资金系统导入";
	}

	@Override
	protected void parseOtherField_FirstStep(com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		if (templeteCode.indexOf("J") != -1) {
			form_type = FORM_JIEKUANDAN;
		} else if (templeteCode.indexOf("B") != -1) {
			form_type = FORM_BAOXIAOFENGMIAN;
		} else if (templeteCode.indexOf("C") != -1) {
			form_type = FORM_CHAILVFEI;
		} else if (templeteCode.indexOf("H") != -1) {
			form_type = FORM_HUIKUANDAN;
		}

		fundsBean.setPk_id(String.valueOf(UUIDLong.longUUID()));
		fundsBean.setOpp_acc_bank(fundsBean.getOpp_acc_bank_type() + fundsBean.getOpp_acc_bank());
		fundsBean.setPk_costsubj(
				templeteCode.indexOf("J") != -1 ? "个人借款" : (templeteCode.indexOf("C") != -1 ? "差旅费" : "3"));
		/**
		 * 1-借款单 2-差旅费 3-报销封面
		 */
		fundsBean.setOperator(
				MyOrgManager.getInstance().getMemberByID(flowExport.getFlowCreatePerson().getId()).getCode());
		log.info(MyOrgManager.getInstance().getMemberByID(flowExport.getFlowCreatePerson().getId()).getCode());
		// 获取姓名
		fundsBean.setName(MyOrgManager.getInstance().getMemberByID(flowExport.getFlowCreatePerson().getId()).getName());
		if (templeteCode.indexOf("B") == -1) {
			fundsBean.setCurr_code("CNY");
		}

	}

	@Override
	protected void parseValueExport(com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = MyUtil.replaceXMLString(ve.getValue());

		if (form_type == FORM_HUIKUANDAN) {
			if ("单据编号".equals(name)) {
				fundsBean.setBill_no(templeteCode.charAt(2) + value);
			} else if ("填写日期".equals(name)) {
				fundsBean.setOperator_date(MyUtil.getDate(value));
			} else if ("汇款期限".equals(name)) {
				fundsBean.setBill_date(MyUtil.getDate(value));
			} else if ("金额小写".equals(name)) {
				//fundsBean.setBill_amount(value);
			oldBill_amount=value;
			} else if ("原已预付金额".equals(name)) {
				fundsBean.setAdv_amount(value);
			} else if ("收款单位".equals(name)) {
				fundsBean.setOpp_corp(value);
				fundsBean.setOpp_acc_name(value);
			} else if ("账号".equals(name)) {
				fundsBean.setOpp_acc_no(value);
			} else if ("开户行1".equals(name)) {
				fundsBean.setOpp_acc_bank_type(value);
			} else if ("分行_支行_分理处".equals(name)) {
				fundsBean.setOpp_acc_bank(value);
			} else if ("凭证张数".equals(name)) {
				fundsBean.setVoucher_count(value);
			} else if ("业务类型1".equals(name)) {
				fundsBean.setCostsubj(value);
			} else if ("所属制造部".equals(name)) {
				pk_corp = value;
			} else if ("汇款事由".equals(name)) {
				fundsBean.setAbs(value);
			}
		} else {
			if ("单据编号".equals(name)) {
				fundsBean.setBill_no(templeteCode.charAt(2) + value);
			} else if ("单位选择器".equals(name)) {
				fundsBean.setPk_corp(OA_UAPAccountContrast.getUapAccountID(value));
				if (templeteCode.indexOf("B") == -1) {
					fundsBean.setOpp_corp(OA_UAPAccountContrast.getAccountNameByOAID(value));
				}
			} else if ("日期".equals(name) || "填写日期".equals(name)) {
				if (templeteCode.indexOf("B") == -1) {
					fundsBean.setBill_date(MyUtil.getDate(value));
				}
				fundsBean.setOperator_date(MyUtil.getDate(value));
			} else if ("付款日期".equals(name) && templeteCode.indexOf("B") != -1) {
				fundsBean.setBill_date(MyUtil.getDate(value));
			} else if ("币种1".equals(name) && templeteCode.indexOf("B") != -1) {
				fundsBean.setCurr_code(value);
			} else if ("小写金额".equals(name) || "金额小写".equals(name)) {	
		//		fundsBean.setBill_amount(value);
			   oldBill_amount=value;
				//合计报销支出 
			}  else if ("合计报销支出".equals(name)) {
				 newBill_amount=value;
			//	fundsBean.setBill_amount(value);
				//合计报销支出 
			}else if ("已预付".equals(name) && templeteCode.indexOf("B") != -1
					|| "预付金额".equals(name) && templeteCode.indexOf("C") != -1) {
				fundsBean.setAdv_amount(value);
			} else if ("借款人".equals(name) || "收款人".equals(name)) {
				fundsBean.setOpp_acc_name(value);
			} else if ("收款单位".equals(name)) {
				fundsBean.setOpp_corp(value);
				fundsBean.setOpp_acc_name(value);
			} else if ("借款人工资卡号".equals(name) || "账号".equals(name)) {
				fundsBean.setOpp_acc_no(value);
			} else if ("开户行1".equals(name)) {
				fundsBean.setOpp_acc_bank_type(value);
			} else if ("分行_支行_分理处_".equals(name)) {
				fundsBean.setOpp_acc_bank(value);
			} else if ("摘要N".equals(name)) {
				fundsBean.setAbs(value);
			} else if ("凭证张数_".equals(name) || "附票据张数".equals(name)) {
				fundsBean.setVoucher_count(value);
			} else if ("报销类型1".equals(name)) {
				fundsBean.setCostsubj(value);
			} else if ("所属制造部".equals(name)) {//||"所属公司".equals(name)
				pk_corp = value;
				log.info(name+"pk_corp:"+pk_corp);
			} else if ("会计审定数".equals(name)) {
				this.p_cost_list.add(value);
			} else if ("费用归属部门下拉框".equals(name)) {
				log.info("p_dept_list:"+value);
				this.p_dept_list.add(value);
			} else if ("费用类别".equals(name)) {
				log.info("p_type_list:"+value);
				this.p_type_list.add(value);
			} else if ("费用归属部门".equals(name)) {
				this.cp_dept_list.add(value);
			} else if ("出差事由".equals(name)) {
				fundsBean.setReasons(value);
			} else if ("项目编号".equals(name)) {
				fundsBean.setProCode(value);
			}
		}
	}
	
	

	@Override
	protected boolean save() {
		log.info("新增"+fundsBean.getBill_no()+"newBill_amount:"+newBill_amount+"oldBill_amount:"+oldBill_amount);
	
		if (fundsBean.getBill_no().indexOf("FM") != -1|| fundsBean.getBill_no().indexOf("CC") != -1) {
			log.info("***"+fundsBean.getBill_no());
			fundsBean.setP_cost_list(this.p_cost_list);
			fundsBean.setP_type_list(this.p_type_list);
			
			if (templeteCode.indexOf("C") != -1) {
				fundsBean.setP_dept_list(this.cp_dept_list);
			}
			if (templeteCode.indexOf("B") != -1) {
				fundsBean.setP_dept_list(this.p_dept_list);
			}
			fundsBean.setBill_amount(newBill_amount);
		}else {
			fundsBean.setBill_amount(oldBill_amount);
		}
		
	/*	if (fundsBean.getOperator().equals("084655") || fundsBean.getOperator().equals("000922")) {
			log.info("测试");
			fundsBean.setP_cost_list(this.p_cost_list);
			fundsBean.setP_type_list(this.p_type_list);
			if (templeteCode.indexOf("C") != -1) {
				fundsBean.setP_dept_list(this.cp_dept_list);
			}
			if (templeteCode.indexOf("B") != -1) {
				fundsBean.setP_dept_list(this.p_dept_list);
			}
		}
*/
		// //非总办、万达、福清巴士，不写入
		// if (MyUtil.indexOf(corpArray, fundsBean.getPk_corp()) == -1) {
		// return true;
		// }

		if ("3".equals(fundsBean.getPk_costsubj())) {
			fundsBean.setPk_costsubj(fundsBean.getCostsubj());
		}
		fundsBean.setPk_costsubj(MyUtil.replaceXMLString(fundsBean.getPk_costsubj()));

		if (pk_corp != null) {
			fundsBean.setPk_corp(pk_corp);
		}

		FundsDao dao = new FundsDao();

		dao.setFundsBean(fundsBean);
		String msg = null;
		if (fundsBean.getOperator() == null || "".equals(fundsBean.getOperator())) {
			msg = new StringBuilder().append("《").append(super.formBean.getFlowExport().getFlowTitle())
					.append("》写入UAP失败，需要手工将数据录入到资金系统。单据编号：").append(fundsBean.getBill_no()).append("。原因：填单人员工号为空。")
					.toString();
		} else {
			try {
				if (dao.save()) {
					return true;
				} else {
					msg = new StringBuilder().append("《").append(super.formBean.getFlowExport().getFlowTitle())
							.append("》写入UAP失败，需要手工将数据录入到资金系统。单据编号：").append(fundsBean.getBill_no())
							.append("。原因：写入数据库异常。").toString();

				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		MessageSender.sendMsg(new long[] { super.formBean.getFlowExport().getFlowCreatePerson().getId(),
				super.formBean.getOperatorID() }, msg);
		LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), msg);
		log.error(msg);
		return false;
	}
}
